//   Copyright 2010 Shantanu Shah<shantanus@gmail.com>
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.shansense.changemodetimer;

import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.widget.Button;
import android.widget.TimePicker;
import android.widget.Toast;

import com.shansense.coretimer.AbsChangeModeTimer;
import com.shansense.coretimer.ChangeModePlugin;

public class VibrateModeTimerPlugin implements ChangeModePlugin {
	public static final int VIBRATE_TAB_INDEX = 1;
	
  private Activity activity;
  private static final String MSG_ENTER_MODE = "Entering Vibrate mode";
  
  public VibrateModeTimerPlugin(Activity activity) {
    this.activity = activity;  
  }
  
  @Override
  public void enterMode() {
    if (!isModeSet()) {
      Toast.makeText(activity, MSG_ENTER_MODE, Toast.LENGTH_SHORT).show();
      AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
      audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
    }
  }

  @Override
  public void leaveModeAndClearNotifications() {
    SilenceModeUtil.leaveModeAndClearNotifications(activity);  // TODO - util
  }

  @Override
  public int getNotificationIcon() {
    return SilenceModeUtil.getNotificationIcon();
  }

  @Override
  public String getNotificationString() {
    return "In vibrate mode till";
  }

  @Override
  public boolean isModeSet() {
  AudioManager audioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
    return audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;
  }

  @Override
  public Intent getNotificationIntent() {
    Intent notificationIntent = new Intent();
    notificationIntent.setAction(Intent.ACTION_MAIN);
    notificationIntent.setClassName(activity, SilenceModeUtil.packageName+ ".SilenceModeTimer");
    return notificationIntent;
  }

  @Override
  public void clearNotifications() {
    NotificationManager nm = (NotificationManager) 
        activity.getSystemService(Context.NOTIFICATION_SERVICE);
    nm.cancel(getNotificationIcon());
  }

  @Override
  public int getAppLayout() {
    //throw new RuntimeException();
    return R.layout.main_silence;
  }

  @Override
  public Button getButtonCancel() {
    //throw new RuntimeException();
    return (Button) activity.findViewById(R.id.buttonNV);
  }

  @Override
  public Button getButtonOK() {
    //throw new RuntimeException();
    return (Button) activity.findViewById(R.id.buttonYV);
  }

  @Override
  public Button getButtonUnset() {
    //throw new RuntimeException();
    return (Button) activity.findViewById(R.id.buttonCancelPreviousV);
  }

  @Override
  public Button getButtonShare() {
    //throw new RuntimeException();
    return (Button) activity.findViewById(R.id.buttonShareV);
  }

  @Override
  public Button getButtonShortcut() {
    //throw new RuntimeException();
    return (Button) activity.findViewById(R.id.buttonShortcutV);
  }
  
  @Override
  public Intent getServiceIntent() {
    return new Intent(activity, SilenceModeTimerService.class);
  }
  
  @Override
  public ComponentName getReceiverComponentName() {
    return SilenceModeBroadcastReceiver.componentName();
  }

  @Override
  public int getApplicationIcon() {
    //throw new RuntimeException();
    return R.drawable.icon_silence; // TODO
  }

  @Override
  public String getShareText() {
    return "I love the SilenceModeTimer app. Get it now: http://is.gd/6S9Er "
        + "Website: http://is.gd/76JrJ";  //TODO?
  }

  @Override
  public Button getButtonMarket() {
    //throw new RuntimeException();
    return  (Button) activity.findViewById(R.id.buttonOtherAppsV);
  }
  
  @Override
  public Button getSavedButton(String savedTimePrefString) {
    //throw new RuntimeException();
    // TODO: wtf, there has to be a better way
    
    if (savedTimePrefString.equals(AbsChangeModeTimer.PREF_SAVED_TIME[1])) {
      return (Button) activity.findViewById(R.id.buttonSaved1V);
    } else if (savedTimePrefString.equals(AbsChangeModeTimer.PREF_SAVED_TIME[2])) {
      return (Button) activity.findViewById(R.id.buttonSaved2V);
    }  else if (savedTimePrefString.equals(AbsChangeModeTimer.PREF_SAVED_TIME[3])) {
      return (Button) activity.findViewById(R.id.buttonSaved3V);
    }     
    return null;  // The "this should never happen" case.
  }

  @Override
  public void hideQuickSelect() {
    //throw new RuntimeException();
    activity.findViewById(R.id.quickSelectV).setVisibility(android.view.View.GONE);
    activity.findViewById(R.id.selectNewV).setVisibility(android.view.View.GONE);
  }

  @Override
  public String getAppName() {
    return SilenceModeUtil.getAppName();
  }

  @Override
  public Button getButtonHelp() {
    //throw new RuntimeException();
    return  (Button) activity.findViewById(R.id.buttonHelpV);
  }

  @Override
  public TimePicker getTimePicker() {
    //throw new RuntimeException();
    return (TimePicker) activity.findViewById(R.id.timepickerV);
  }
  
  @Override
  public Activity getActivity() {
    return activity;
  }

	@Override
  public int getTabIndexIfPresent() {
	  return VIBRATE_TAB_INDEX;
  }

	@Override
  public String getShortcutName() {
	  return "VibrateModeTimer";
  }
}
